package com.hongmeng.teamwork.factory.task;

import com.hongmeng.teamwork.entity.dto.MessageDto;
import com.hongmeng.teamwork.entity.dto.TaskMessageDto;
import com.hongmeng.teamwork.entity.dto.TeamworkTaskDto;
import com.hongmeng.teamwork.entity.po.TeamworkTask;
import com.hongmeng.teamwork.enums.TaskOptTypeEnum;
import com.hongmeng.teamwork.enums.TaskStatusEnum;
import com.hongmeng.teamwork.tempalte.MessageSendTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.LinkedList;

@Service
public class TaskUpdateHandler implements TaskOptHandler {

    @Override
    public TaskOptTypeEnum getOptType() {
        return TaskOptTypeEnum.UPDATE;
    }

    @Override
    public String buildMessage(TaskMessageDto taskMessageDto) {
        TeamworkTaskDto teamworkTaskDto = taskMessageDto.getTeamworkTaskDto();
        TeamworkTask teamworkTask = taskMessageDto.getTeamworkTask();
        LinkedList<LocalDateTime> startDateTimeList = new LinkedList<>();
        LinkedList<LocalDateTime> endDateTimeList = new LinkedList<>();
        if (!teamworkTask.getStartTime().isEqual(teamworkTaskDto.getStartTime())) {
            startDateTimeList.add(teamworkTask.getStartTime());
            startDateTimeList.add(teamworkTaskDto.getStartTime());
        }
        if (!teamworkTask.getEndTime().isEqual(teamworkTaskDto.getEndTime())) {
            endDateTimeList.add(teamworkTask.getEndTime());
            endDateTimeList.add(teamworkTaskDto.getEndTime());
        }

        return MessageSendTemplate.updateTaskTemplate(teamworkTaskDto.getUpdatedUser().getName(),
                teamworkTask.getName(),
                taskMessageDto.getProjectName(),
                taskMessageDto.getOldLeaderUser(),
                teamworkTaskDto.getLeadUser().getName(),
                teamworkTask.getDescription(),
                startDateTimeList,
                endDateTimeList,
                TaskStatusEnum.ofEnum(teamworkTask.getStatus()),
                TaskStatusEnum.ofEnum(teamworkTaskDto.getStatus())
                );
    }
}
